<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <!-- <el-form-item label="创建时间" prop="createTime">
        <el-date-picker v-model="queryParams.createTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" />
      </el-form-item>
      <el-form-item label="openId" prop="openId">
        <el-input v-model="queryParams.openId" placeholder="请输入openId" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item> -->
      <el-form-item label="昵称" prop="nickName">
        <el-input v-model="queryParams.nickName" placeholder="请输入昵称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入手机号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="是否开通vip" prop="isVip">
        <!-- <el-input v-model="queryParams.isVip" placeholder="请输入是否开通vip(1是2否)" clearable @keyup.enter.native="handleQuery"/> -->
         <el-select v-model="queryParams.isVip" placeholder="请选择">
            <el-option label="是" :value="1"></el-option>
            <el-option label="否" :value="2"></el-option>
         </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
<!--    <el-row :gutter="10" class="mb8">-->
<!--      <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="setVIP">会员折扣比例配置</el-button>-->
<!--    </el-row>-->

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
       <el-table-column label="id" align="center" prop="id" />
      <!-- <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="openId" align="center" prop="openId" /> -->
      <el-table-column label="昵称" align="center" prop="nickName" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="邮箱" align="center" prop="mail" />
      <el-table-column label="是否开通vip" align="center" prop="isVip">
        <template v-slot="scope">
          <span>{{ scope.row.isVip ===1 ?'是':scope.row.isVip ===2 ?'否' :''}}</span>
        </template>
      </el-table-column>
      <el-table-column label="vip开通时间" align="center" prop="vipTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.vipTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="vip到期时间" align="center" prop="vipEndTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.vipEndTime) }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="推荐人" align="center" prop="shopName" />-->
      <el-table-column label="头像" align="center" prop="avatar" width="110">
        <template v-slot="scope">
          <img style="width:100px;height:100px" :src="scope.row.avatar" alt="">
        </template>
      </el-table-column>
      <!-- <el-table-column label="搜索记录" align="center" prop="searchs" /> -->
<!--      <el-table-column label="会员码" align="center" prop="vipCode" />-->
      <el-table-column label="抵扣金额" align="center" prop="deductionPrice" />
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['qd:user:update']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['qd:user:delete']">删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <el-dialog title="会员折扣比例配置" :visible.sync="openvip" width="500px" v-dialogDrag append-to-body>
      <div style="display:flex;align-items:center">
        <p style="white-space: nowrap;width:128px">商品折扣比例：</p>
        <el-input v-model="vipvalue.discount"  type="number" placeholder="请输入会员折扣比例" />
        <p>%</p>
      </div>
      <div style="display:flex;align-items:center">
        <p style="white-space: nowrap;width:128px">普通会员折扣比例：</p>
        <el-input v-model="vipvalue.oneDiscount"  type="number" placeholder="请输入普通会员折扣比例" />
        <p>%</p>
      </div>
      <div style="display:flex;align-items:center">
        <p style="white-space: nowrap;width:128px;">会员价格：</p>
        <el-input v-model="vipvalue.price" type="number" placeholder="请输入会员价格" />
        <p>元</p>
      </div>
      <div style="display:flex;align-items:center">
        <p style="white-space: nowrap;width:128px;">VIP体验价格：</p>
        <el-input v-model="tivipvalue.price" type="number" placeholder="请输入VIP体验价格" />
        <p>元</p>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="setVIPcomfirm">确 定</el-button>
        <el-button @click="openvip = false">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="500px" v-dialogDrag append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="openId" prop="openId">
          <el-input v-model="form.openId" placeholder="请输入openId" />
        </el-form-item>
        <el-form-item label="昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="是否开通vip(1是2否)" prop="isVip">
          <el-input v-model="form.isVip" placeholder="请输入是否开通vip(1是2否)" />
        </el-form-item>
        <el-form-item label="vip开通时间" prop="vipTime">
          <el-date-picker clearable v-model="form.vipTime" type="date" value-format="timestamp" placeholder="选择vip开通时间" />
        </el-form-item>
        <el-form-item label="头像" prop="avatar">
          <el-input v-model="form.avatar" placeholder="请输入头像" />
        </el-form-item>
        <el-form-item label="搜索记录" prop="searchs">
          <el-input v-model="form.searchs" placeholder="请输入搜索记录" />
        </el-form-item>
        <el-form-item label="会员码" prop="vipCode">
          <el-input v-model="form.vipCode" placeholder="请输入会员码" />
        </el-form-item>
        <el-form-item label="抵扣金额" prop="deductionPrice">
          <el-input v-model="form.deductionPrice" placeholder="请输入抵扣金额" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { createUser, updateUser, deleteUser, getUser, getUserPage, exportUserExcel } from "@/api/qd/user";

export default {
  name: "User",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 小程序-用户列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        createTime: [],
        openId: null,
        nickName: null,
        phone: null,
        isVip: null,
        vipTime: [],
        avatar: null,
        searchs: null,
        vipCode: null,
        deductionPrice: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        openId: [{ required: true, message: "openId不能为空", trigger: "blur" }],
      },
      vipvalue:{}, // 会员
      tivipvalue:{}, // 体验会员
      openvip:false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    setVIP(){
      this.$axios({url:'/qd/discount/selectAll',method:'get'}).then(res=>{

        this.$set(this,'vipvalue',res.data[0])
        this.$set(this,'tivipvalue',res.data[1])
        this.openvip = true
      })
    },
    setVIPcomfirm(){
      if(!this.vipvalue.discount) return this.$message.warning("请先输入会员折扣比例")
      this.tivipvalue.discount = this.vipvalue.discount
      this.$axios({url:'/qd/discount/update',method:'put',data:this.vipvalue}).then(res=>{
        // this.$message.success("设置成功")
        // this.openvip = false
      })
      this.$axios({url:'/qd/discount/update',method:'put',data:this.tivipvalue}).then(res=>{
        this.$message.success("设置成功")
        this.openvip = false
      })
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getUserPage(this.queryParams).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        openId: undefined,
        nickName: undefined,
        phone: undefined,
        isVip: undefined,
        vipTime: undefined,
        avatar: undefined,
        searchs: undefined,
        vipCode: undefined,
        deductionPrice: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加小程序-用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getUser(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改小程序-用户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateUser(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createUser(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除?').then(function() {
          return deleteUser(id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal.confirm('是否确认导出所有小程序-用户数据项?').then(() => {
          this.exportLoading = true;
          return exportUserExcel(params);
        }).then(response => {
          this.$download.excel(response, '小程序-用户.xls');
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
